package com.rpa.server.servicerpaserver.expose.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rpa.secure.servicerpasecure.common.result.Result;
import com.rpa.server.servicerpaserver.crawler.entity.CeekSetting;
import com.rpa.server.servicerpaserver.crawler.service.CeekSettingService;
import com.rpa.server.servicerpaserver.crawler.service.ScoreService;
import com.rpa.server.servicerpaserver.expose.dto.CeekSettingDto;
import com.rpa.server.servicerpaserver.expose.dto.ScoreDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @className: CeekSettingController
 * @author: lzt
 * @date: 2025/2/17 10:39
 * @Version: 1.0
 * @description:
 */
@RestController
@RequestMapping("/api/ceekSetting")
@Slf4j
@Tag(name = "本系统签到规则设置相关接口", description = "CeekSettingController")
public class CeekSettingController {
    @Autowired
    private CeekSettingService ceekSettingService;
    @Autowired
    private ScoreService scoreService;

    @PostMapping("/getCeekSetting")
    @Operation(summary = "获取签到规则设置", description = "getCeekSetting")
    public Result<CeekSetting> getCeekSetting(@RequestBody CeekSettingDto ceekSettingDto) {
        log.info("获取签到规则设置");
        if (Objects.isNull(ceekSettingDto)) {
            return Result.error("参数错误");
        }
        LambdaQueryWrapper<CeekSetting> eq = new LambdaQueryWrapper<CeekSetting>()
                .eq(CeekSetting::getAccountId, ceekSettingDto.getAccountId())
                .eq(CeekSetting::getCourseId, ceekSettingDto.getCourseId())
                .eq(CeekSetting::getClazzId, ceekSettingDto.getClazzId());
        CeekSetting one = ceekSettingService.getOne(eq);
        return Result.success(one);
    }

    @PostMapping("/updateCeekSetting")
    @Operation(summary = "更新签到规则设置", description = "updateCeekSetting")
    public Result<Boolean> updateCeekSetting(@RequestBody CeekSetting ceekSetting) {
        if (Objects.isNull(ceekSetting)) {
            return Result.error("参数错误");
        }
        boolean b = ceekSettingService.updateById(ceekSetting);
        if (b) {
            ScoreDto scoreDto = new ScoreDto();
            BeanUtil.copyProperties(ceekSetting, scoreDto);
            scoreDto.setResetType("2");
             b = scoreService.resetScore(scoreDto)&&b;
        }
        return Result.success(b);
    }
}
